﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Walson.Form.FlightDevice.PFD
{
    public partial class PrimaryFlightDisplay : Control
    {
        /// <summary>
        /// 倾斜角度
        /// </summary>
        public double Bank
        {
            get
            {
                return pab.Bank;
            }
            set
            {
                pab.Bank = value;
            }
        }

        /// <summary>
        /// 俯仰角度
        /// </summary>
        public double Pitch
        {
            get
            {
                return pab.Pitch;
            }
            set
            {
                pab.Pitch = value;
            }
        }

        /// <summary>
        /// 海拔高度，单位为米
        /// </summary>
        public double Altitude
        {
            get
            {
                return nmiAltitude.Value;
            }
            set
            {
                nmiAltitude.Value = value;
                nmiAltitude.GroundAltitudeValue = this.Altitude - this.HeightFromGround;
            }
        }

        /// <summary>
        /// 空速
        /// </summary>
        public double AirSpeed
        {
            get
            {
                return nmiAirSpeed.Value;
            }
            set
            {
                nmiAirSpeed.Value = value;
            }
        }

        /// <summary>
        /// 最大侧倾限制
        /// </summary>
        public double BankLimit
        {
            get
            {
                return pab.BankLimit;
            }
            set
            {
                pab.BankLimit = value;
            }
        }

        /// <summary>
        /// 预设的海拔高度
        /// </summary>
        public double DestinationAltitude
        {
            get
            {
                return nmiAltitude.SetValue;
            }
            set
            {
                nmiAltitude.SetValue = value;
               // lblDestinationAltitude.Text = nmiAltitude.SetValue.ToString("f0");
            }
        }

        /// <summary>
        /// 地面海拔高度
        /// </summary>
        private double GroundAltitude
        {
            get
            {
                return nmiAltitude.GroundAltitudeValue;
            }
            set
            {
                nmiAltitude.GroundAltitudeValue = value;
            }
        }

        /// <summary>
        /// 预设的空速
        /// </summary>
        public double DestinationSpeed
        {
            get
            {
                return nmiAirSpeed.SetValue;
            }
            set
            {
                if (value >= 0)
                {
                    nmiAirSpeed.SetValue = value;
                    //lblDestinationAirSpeed.Text = nmiAirSpeed.SetValue.ToString("f0");
                }
            }
        }

        /// <summary>
        /// 航向
        /// </summary>
        public double Heading
        {
            get
            {
                return compass.Heading;
            }
            set
            {
                compass.Heading = value;
            }
        }

        /// <summary>
        /// 是否超速
        /// </summary>
        public bool OverSpeed
        {
            get
            {
                return pab.OverSpeed;
            }
            set
            {
                pab.OverSpeed = value;
            }
        }

        /// <summary>
        /// 是否失速
        /// </summary>
        public bool Stall
        {
            get
            {
                return pab.Stall;
            }
            set
            {
                pab.Stall = value;
            }
        }

        /// <summary>
        /// 垂直速度
        /// </summary>
        public double VerticalSpeed
        {
            get
            {
                return vsi.VerticalSpeed;
            }
            set
            {
                vsi.VerticalSpeed = value;
                //lblPositiveVerticalSpeed.Text = vsi.VerticalSpeed.ToString("f2");
                //lblNegtiveVerticalSpeed.Text = vsi.VerticalSpeed.ToString("f2");
                if (vsi.VerticalSpeed > 0.5)
                {
                  //  lblPositiveVerticalSpeed.Visible = true;
                    //lblNegtiveVerticalSpeed.Visible = false;
                }
                else if (vsi.VerticalSpeed < -0.5)
                {
                  //  lblPositiveVerticalSpeed.Visible = false;
                  //  lblNegtiveVerticalSpeed.Visible = true;
                }
                else
                {
                   // lblPositiveVerticalSpeed.Visible = false;
                   // lblNegtiveVerticalSpeed.Visible = false;
                }
            }
        }

        /// <summary>
        /// 距离地面的高度。
        /// </summary>
        public double HeightFromGround
        {
            get
            {
                return pab.HeightFromGround;
            }
            set
            {
                pab.HeightFromGround = value;
                nmiAltitude.GroundAltitudeValue = this.Altitude - this.HeightFromGround;
                //GroundAltitude -= value / 100;
            }
        }

        /// <summary>
        /// 危险高度，低于该高度时PFD上将显示当前距地面的高度。
        /// </summary>
        public int CautionHeight
        {
            get
            {
                return pab.DangerHeight;
            }
            set
            {
                pab.DangerHeight = value;
            }
        }

        public PrimaryFlightDisplay()
        {
            InitializeComponent();

            this.AirSpeed = 0;
            this.Altitude = 0;
            this.Bank = 0;
            this.BankLimit = 45;
            this.DestinationAltitude = 0;
            this.DestinationSpeed = 0;
            this.GroundAltitude = 0;
            this.Heading = 0;
            this.OverSpeed = false;
            this.Pitch = 0;
            this.Stall = false;
            this.VerticalSpeed = 0;

            this.SizeChanged += new EventHandler(PrimaryFlightDisplay_SizeChanged);
        }

        void PrimaryFlightDisplay_SizeChanged(object sender, EventArgs e)
        {
            vsi.Width = (int)(this.Width * 0.078);
            vsi.Height = (int)(this.Height * 0.8);

            nmiAirSpeed.Width = (int)(this.Width * 0.1);
            nmiAirSpeed.Height = (int)(this.Height * 0.85);

            nmiAltitude.Width = (int)(this.Width * 0.1);
            nmiAltitude.Height = (int)(this.Height * 0.85);

            pab.Width = (int)(this.Width * 0.53);
            pab.Height = (int)(this.Height * 0.74);

            compass.Width = pab.Width;
            compass.Height = (int)(this.Height * 0.113);

            vsi.Invalidate();
            nmiAirSpeed.Invalidate();
            nmiAltitude.Invalidate();
            pab.Invalidate();
            compass.Invalidate();

            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            vsi.Invalidate();
            nmiAltitude.Invalidate();
            pab.Invalidate();
            compass.Invalidate();

            int width = nmiAirSpeed.Width + pab.Width + nmiAltitude.Width + vsi.Width+50;
            int height = pab.Height + compass.Height + 50;
            float scaleFactorX = this.Width / width;
            float scaleFactorY = this.Height / height;
            using (Bitmap bmp = new Bitmap(width, height))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    //g.ScaleTransform(scaleFactorX, scaleFactorY);

                    g.DrawImage(nmiAirSpeed.Image, 0, (height - nmiAirSpeed.Height) / 2);

                    g.DrawImage(pab.Image, (width-pab.Width)/2, (height - pab.Height) / 2);

                    g.DrawImage(nmiAltitude.Image, nmiAirSpeed.Width + 5 + pab.Width + 5, (height - nmiAltitude.Height) / 2);

                    g.DrawImage(vsi.Image, nmiAirSpeed.Width + 5 + pab.Width + 5 + nmiAltitude.Width + 5, (height - vsi.Height) / 2);

                    g.DrawImage(compass.Image, (width - compass.Width) / 2, (height - pab.Height) / 2 + 5);

                    g.DrawString("FFF", this.Font, Brushes.White, new PointF(10, 10));
                }
                pe.Graphics.DrawImage(bmp, 0, 0);
            }

            base.OnPaint(pe);
        }
    }
}
